package br.com.iswe.gwt.ui.client.chart;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;

public class ChartUtils {

	private static final Map playerProps = new HashMap();
	private static final Map playerAttr  = new HashMap();
	static{
		//carrega as propriedades padroes
		playerAttr.put("codebase", 				"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0");
		playerAttr.put("align", 				"middle");
		playerAttr.put("classid", 				"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000");
		
		//Atributos
		playerProps.put("quality", 				"high");
		playerProps.put("pluginspage", 			"http://www.macromedia.com/go/getflashplayer");
		playerProps.put("play", 				"true");
		playerProps.put("loop", 				"true");
		playerProps.put("scale", 				"showall");
		playerProps.put("wmode", 				"window");
		playerProps.put("devicefont", 			"false");
		playerProps.put("menu", 				"true");
		playerProps.put("allowFullScreen", 		"false");
		playerProps.put("allowScriptAccess", 	"sameDomain");
		playerProps.put("salign", "");
	}
	
	public static DialogBox createBar2DChart(String titulo, Chart2DSeries series) {
		DialogBox dialogBox = new DialogBox(true);
		String chartFileName = "Chart_Bar2d";
		String width = "500";
		String height = "320";
		//Configura o Filme
		//Atributos
		playerAttr.put("width", 	width);
		playerAttr.put("height", 	height);
		playerAttr.put("id", 		chartFileName);
		playerAttr.put("name", 		chartFileName);
		
		//Propriedades
		playerProps.put("FlashVars","chartSeries=" + series.toString());
		playerProps.put("bgcolor", 	"#ffffff");
		playerProps.put("movie", 	"charts/" + chartFileName + ".swf");
		playerProps.put("src", 		"charts/" + chartFileName + ".swf");
		//"Janeiro->462|Fevereiro->857|Marco->671"
		HTML divChart = new HTML(buildFlashHtml());
		dialogBox.setWidget(divChart);
		dialogBox.setSize(width + "px", height + "px");
		dialogBox.setText(titulo);
		return dialogBox;
	}

	private static String buildFlashHtml() {
		String html = "";
		if (isIE() && isWin() && !isOpera()){
			html += "<object ";
			Iterator it = playerAttr.entrySet().iterator();
		    while(it.hasNext()){
		    	Map.Entry entry = (Map.Entry)it.next();
		    	html += (String)entry.getKey() + "=\"" + (String)entry.getValue() + "\" ";
		    }
		    html += ">\n";
		    it = playerProps.entrySet().iterator();
		    while(it.hasNext()){
		    	//ignora o src
		    	Map.Entry entry = (Map.Entry)it.next();
		    	if("src".equals(entry.getKey())){
		    		continue;
		    	}
		    	html += "	<param name=\"" + (String)entry.getKey() + "\" value=\"" + (String)entry.getValue() + "\" /> \n";
		    }
		    html += "</object>";
		} 
		else {
			html += "<embed ";
			Iterator it = playerAttr.entrySet().iterator();
		    while(it.hasNext()){
		    	Map.Entry entry = (Map.Entry)it.next();
		    	html += (String)entry.getKey() + "=\"" + (String)entry.getValue() + "\" ";
		    }
		    it = playerProps.entrySet().iterator();
		    while(it.hasNext()){
		    	Map.Entry entry = (Map.Entry)it.next();
		    	html += (String)entry.getKey() + "=\"" + (String)entry.getValue() + "\" ";
		    }
		    html += " > </embed>";
		  }

		return html;
	}
	
	private native static boolean isIE()/*-{
		return (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	}-*/;
	private native static boolean isWin()/*-{
		return (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	}-*/;
	private native static boolean isOpera()/*-{
		return (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
	}-*/;

}
